//
//  HWMineHttpManager.swift
//  HWanGame
//
//  Created by 王昌健 on 2021/12/22.
//  Copyright © 2021 94Wan. All rights reserved.
//

import UIKit
import Alamofire
import AdSupport
class HWMineHttpManager: NSObject {
    var uid = HWLocallyStoredData.getLoginUserID()
    var tStr = "\(WMUtil.getTimeSp())\(WMUtil.getRandomNumber(101, to: 998))"
    var p = 1
    var max_p = "1"
}
extension HWMineHttpManager {
    /// sign加密
    /// - Returns: sign
    func encrySign() -> String {
        
        let sign = "\(uid)\(tStr)\("fsd213ewdsadqwe2121213edsad")"
        let signMD5 = WMUtil.md5HexDigest(sign) ?? ""
        return signMD5
    }
    func headers() -> HTTPHeaders {
//        let tStr = Date().timeStamp
//        let signature = "nnmitu"+tStr
//
//        let json = ["usersid":HWLocallyData.getUsersID(),"timestamp":tStr,"signature":signature.DDMD5Encrypt(.lowercase32)]
//        let jsonString = HWPublicFuncTools.convertDictionAryToJson(dic: json as NSDictionary)
//        let aesStr:String = aesEncryptString(jsonString, "NrrynIXcpucYvGSk")
        let header:HTTPHeaders = ["eaua":JJUUIDManager.getUUID(),"imei":ASIdentifierManager.shared().advertisingIdentifier.uuidString]
        
        return header
    }
//    10454
//    func tString() -> String {
//        return "\(WMUtil.getTimeSp())\(WMUtil.getRandomNumber(101, to: 998))"
//    }
}

extension HWMineHttpManager {
    func getMineHomeInfo(success:@escaping (_ :HWMineModel) -> Void, failure:@escaping(_ :String)->Void){
        tStr = "\(WMUtil.getTimeSp())\(WMUtil.getRandomNumber(101, to: 998))"
        uid = HWLocallyStoredData.getLoginUserID()
        let parameters  = [
            "uid":uid,
            "t":tStr,
            "sign":encrySign(),
            "ios":"1",
            "member_id":HWLocallyStoredData.getCode()
        ] as [String : Any]
        
        AF.request(URL(string: URL_MineInfo)!, method: .post, parameters: parameters, headers: headers()).responseString { (responses) in
            if responses.value != nil {
                
                let result:String = responses.value ?? ""
                if result != "" {
                    
                    let resultDic = HWUniversal.JSONStringToDictionary(jsonString: result)
                    if resultDic["code"] as? Int == 1 {
                        
                        
                        print("***********\(HWUniversal.JSONStringToDictionary(jsonString: result))")
                        if let model = HWMineModel.deserialize(from: result ,designatedPath: ".data") {
                            print(model)
                            success(model)
                            
                        }
                        
                        
                    } else if resultDic["code"] as? Int == 100 {
                        HWUniversal.reLogin(message: (resultDic["message"] as? String) ?? "")
                    }else {
                        
                        print("请求失败--原因:\(resultDic["message"] ?? "请求失败--未知原因")")
                        failure(resultDic["message"] as? String ?? "")
                    }
                    
                }else {
                    print("没有获取到数据")
                    
                }
                
            }else {
                print("获取数据失败")
                //                self.p -= 1
            }
        }
    }
    
    func getMineNewMessageInfo(success:@escaping (_ :Bool) -> Void, failure:@escaping(_ :String)->Void){
        tStr = "\(WMUtil.getTimeSp())\(WMUtil.getRandomNumber(101, to: 998))"
        uid = HWLocallyStoredData.getLoginUserID()
        let parameters  = [
            "uid":uid,
            "t":tStr,
            "sign":encrySign(),
            "member_id":HWLocallyStoredData.getCode(),
            "ios":"1",
            "Version":"3.0"
        ] as [String : Any]
        
        AF.request(URL(string: URL_MineNewMessage)!, method: .post, parameters: parameters, headers: headers()).responseString { (responses) in
            if responses.value != nil {
                
                let result:String = responses.value ?? ""
                if result != "" {
                    
                    let resultDic = HWUniversal.JSONStringToDictionary(jsonString: result)
                    if resultDic["code"] as? Int == 1 {
                        
                        success(true)
                        print("***********\(HWUniversal.JSONStringToDictionary(jsonString: result))")
//                        if let model = HWMineModel.deserialize(from: result ,designatedPath: ".data") {
//                            print(model)
//                            success(model)
//
//                        }
                        
                        
                    } else if resultDic["code"] as? Int == 100 {
                        HWUniversal.reLogin(message: (resultDic["message"] as? String) ?? "")
                    }else {
                        
                        print("请求失败--原因:\(resultDic["message"] ?? "请求失败--未知原因")")
                        failure(resultDic["message"] as? String ?? "")
                    }
                    
                }else {
                    print("没有获取到数据")
                    
                }
                
            }else {
                print("获取数据失败")
                //                self.p -= 1
            }
        }
    }
    
    func getMyAssetsInfo(success:@escaping (_ :Bool) -> Void, failure:@escaping(_ :String)->Void){
        tStr = "\(WMUtil.getTimeSp())\(WMUtil.getRandomNumber(101, to: 998))"
        uid = HWLocallyStoredData.getLoginUserID()
        let parameters  = [
            "uid":uid,
            "t":tStr,
            "sign":encrySign(),
            "member_id":HWLocallyStoredData.getCode(),
            "ios":"1",
            "Version":"3.0"
        ] as [String : Any]
        
        AF.request(URL(string: URL_MyAssetsInfo)!, method: .post, parameters: parameters, headers: headers()).responseString { (responses) in
            if responses.value != nil {
                
                let result:String = responses.value ?? ""
                if result != "" {
                    
                    let resultDic = HWUniversal.JSONStringToDictionary(jsonString: result)
                    if resultDic["code"] as? Int == 1 {
                        let dic = resultDic["data"] as? NSDictionary
                        if dic?["top"] as? Int  == 1{
                            success(true)
                        }else{
                            success(false)
                        }
                        
                        print("***********\(HWUniversal.JSONStringToDictionary(jsonString: result))")
//                        if let model = HWMineModel.deserialize(from: result ,designatedPath: ".data") {
//                            print(model)
//                            success(model)
//
//                        }
                        
                        
                    } else if resultDic["code"] as? Int == 100 {
                        HWUniversal.reLogin(message: (resultDic["message"] as? String) ?? "")
                    }else {
                        
                        print("请求失败--原因:\(resultDic["message"] ?? "请求失败--未知原因")")
                        failure(resultDic["message"] as? String ?? "")
                    }
                    
                }else {
                    print("没有获取到数据")
                    
                }
                
            }else {
                print("获取数据失败")
                //                self.p -= 1
            }
        }
    }
}
